package com.lj.tools.oss;


import com.lj.tools.exception.YerException;
import com.lj.tools.util.FileUtil;
import com.lj.tools.util.Md5Util;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

//@ApiModel
@Data
public class UploadFile {
    //@ApiModelProperty(name = "路径")
    //@TableId(type = IdType.INPUT)
    private String path;
    //@ApiModelProperty(name = "名称")
    private String name;
    //@ApiModelProperty(name = "文件后缀", notes = "图片/文档/视频/音频/压缩包/可执行文件")
    private String ext;
    //@ApiModelProperty(name = "文件大小")
    private Long size;
    //@ApiModelProperty(name = "文件md5值")
    private String md5;
    //@ApiModelProperty(name = "是否临时文件")
    private Boolean isTmpFile = true;
    //@ApiModelProperty(name = "文件过期日期")
    private Date expiredTime;
    //@ApiModelProperty(name = "是否删除")
    private Boolean isDel = false;
    //@ApiModelProperty(name = "最后更新日期")
    private Date lastTime;
    //@ApiModelProperty(name = "创建日期")
    private Date createTime = new Date();


    public static UploadFile createFromMultiFile(MultipartFile multipartFile) throws IOException, NoSuchAlgorithmException {
        return createFromMultiFile(multipartFile, null);
    }


    public static UploadFile createFromMultiFile(MultipartFile multipartFile, Boolean isTmpFile) throws IOException, NoSuchAlgorithmException {
        UploadFile uploadFile = new UploadFile();

        byte[] md5Bytes = Md5Util.computeMD5Hash(multipartFile.getInputStream());
        uploadFile.setMd5(Md5Util.toHex(md5Bytes));

        String fileName = multipartFile.getOriginalFilename();
        uploadFile.setName(fileName);
        uploadFile.setExt(FileUtil.getExtension(fileName));
        uploadFile.setSize(multipartFile.getSize());
        if (Objects.isNull(isTmpFile) || isTmpFile){
            uploadFile.setIsTmpFile(true);
            uploadFile.setAutoDel();
        }else {
            uploadFile.setIsTmpFile(false);
        }
        return uploadFile;
    }

    /**
     * 检查是否符合安全规则
     * */
    public void checkSafe() throws YerException {
        String[] allowedFileExtension = {"txt", "jpeg", "jpg", "png", "bmp", "gif",
                "doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf",
                "zip", "gz", "rar", "7z"};

        for (String ext:allowedFileExtension ) {
            if (ext.equals(getExt())){
                return;
            }
        }
        throw new YerException("文件不符合安全规则，不允许上传");
    }


    public void setAutoDel(){
        setIsTmpFile(true);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, 24);
        setExpiredTime(calendar.getTime());
    }


    /**
     * 下载文件数据库
     * */
    public void download(BufferedOutputStream outputStream, String clientHost) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(clientHost+getPath());
        byte[] bytes = new byte[1024];
        int count = 0;
        while (true){
            count = fileInputStream.read(bytes);
            if (count == -1 || count == 0){
                break;
            }
            outputStream.write(bytes, 0, count);
            outputStream.flush();
        }
        outputStream.close();
    }
}
